﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using Fatec.ControleEstoque.Attributes;
using Fatec.ControleEstoque.Data.Model;
using System.ComponentModel;
using Fatec.ControleEstoque.Data.CustomModel;

namespace Fatec.ControleEstoque.Interface
{
    public partial class Insert_Pedidos : System.Web.UI.Page
    {
        protected MetaTable table;
        protected MetaTable table2;
                       
        protected Pedidos tempPedidos;

        protected void Page_Init(object sender, EventArgs e)
        {
            DetailsDataSource.ContextType = typeof(EstoqueEntities);
            
            DynamicDataManager1.RegisterControl(DetailsView1);
            DynamicDataManager1.RegisterControl(DetailsView2);
            DynamicDataManager1.RegisterControl(GridView1);

            table = DetailsDataSource.GetTable();
            //table2 = ObjectDataSource1.FindMetaTable();
                       
            DetailsView1.RowsGenerator = new HideColumnFieldsManager(table, PageTemplate.Insert);
            //DetailsView2.RowsGenerator = new HideColumnFieldsManager(table2, PageTemplate.Insert);
            //GridView1.ColumnsGenerator = new HideColumnFieldsManager(table2, PageTemplate.Insert);
        }

        protected void Page_Load(object sender, EventArgs e)
        {         
            table = DetailsDataSource.GetTable();
            //table2 = ObjectDataSource1.FindMetaTable();

            GridView1.DataBind();

            Title = table.DisplayName;

            DetailsDataSource.Include = table.ForeignKeyColumnsNames;
            // EntityDataSource1.Include = table2.ForeignKeyColumnsNames;
        }

        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == DataControlCommands.CancelCommandName)
            {
                Response.Redirect(table.ListActionPath);
            }
        }

        private void SetDeleteConfirmation(TableRow row)
        {
            foreach (Control c in row.Cells[0].Controls)
            {
                if (c is LinkButton)
                {
                    LinkButton btn = (LinkButton)c;
                    if (btn.CommandName == DataControlCommands.DeleteCommandName)
                    {
                        btn.OnClientClick = "return confirm('Você tem certeza que deseja excluir este item?');";
                    }
                }
            }
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception == null || e.ExceptionHandled)
            {
                Response.Redirect(table.ListActionPath);
            }
        }

        protected void LinkButtonInsert_Click(object sender, EventArgs e)
        {
            DetailsView2.ChangeMode(DetailsViewMode.Insert);

            DetailsView2.DataBind();
            UpdatePanel2.Update();
            ModalPopupExtender1.Show();
        }

        protected void LinkButtonSave_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                if (DetailsView2.CurrentMode == DetailsViewMode.Insert)
                {
                    
                    DetailsView2.InsertItem(false);
                }
                else
                {
                    DetailsView2.UpdateItem(false);
                }
                GridView1.DataBind();
                ModalPopupExtender1.Hide();
            }
        }

        protected void DetailsDataSource_DataBinding(object sender, EventArgs e)
        {
        }

        protected void DetailsView1_DataBound(object sender, EventArgs e)
        {
            ICustomTypeDescriptor descriptor = DetailsView1.DataItem as ICustomTypeDescriptor;

            if (descriptor != null && tempPedidos == null)
            {
                var prop = descriptor.GetProperties().Cast<PropertyDescriptor>().First();

                tempPedidos = (Pedidos)descriptor.GetPropertyOwner(prop);


                Session["tempPedidos"] = tempPedidos;
            }
        }

        protected void DetailsDataSource_Inserted(object sender, EntityDataSourceChangedEventArgs e)
        {
            if (Session["tempPedidos"] != null)
            {
                Session.Remove("tempPedidos");
            }
        }

        protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
        {
            // ((Pedido_Produtos)e.Entity).PedidosReference.EntityKey = new EntityKey("Pedidos", "Id", null);
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            SetDeleteConfirmation(e.Row);
        }
    }
}
